package com.jlwl.houseservice.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jlwl.houseservice.common.Result;
import com.jlwl.houseservice.entity.Review;
import com.jlwl.houseservice.service.ReviewService;
import com.jlwl.houseservice.vo.ReviewCreateVO;
import com.jlwl.houseservice.vo.ReviewQueryVO;
import com.jlwl.houseservice.vo.ReviewStatsVO;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

/**
 * 评价控制器
 */
@RestController
@RequestMapping("/reviews")
public class ReviewController {

    @Autowired
    private ReviewService reviewService;

    /**
     * 获取评价列表
     */
    @GetMapping
    public Result<Page<Review>> list(ReviewQueryVO queryVO) {
        Page<Review> page = reviewService.pageList(queryVO);
        return Result.success(page);
    }

    /**
     * 创建评价
     */
    @PostMapping
    @PreAuthorize("hasRole('USER')")
    public Result<Void> create(@Valid @RequestBody ReviewCreateVO createVO) {
        // 获取当前用户ID
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        
        // 从用户名中提取用户ID（实际项目中应该从UserDetails中获取）
        Integer userId = getUserIdFromUsername(username);
        
        // 检查是否已评价
        if (reviewService.hasReviewed(createVO.getOrderNo(), userId)) {
            return Result.error("您已经评价过该订单");
        }
        
        // 创建评价
        boolean success = reviewService.createReview(createVO, userId);
        if (!success) {
            return Result.error("创建评价失败");
        }
        
        return Result.success();
    }

    /**
     * 获取服务评价统计
     */
    @GetMapping("/stats/service/{serviceId}")
    public Result<ReviewStatsVO> getServiceReviewStats(@PathVariable Integer serviceId) {
        ReviewStatsVO statsVO = reviewService.getServiceReviewStats(serviceId);
        return Result.success(statsVO);
    }

    /**
     * 获取员工评价统计
     */
    @GetMapping("/stats/employee/{employeeId}")
    public Result<ReviewStatsVO> getEmployeeReviewStats(@PathVariable Integer employeeId) {
        ReviewStatsVO statsVO = reviewService.getEmployeeReviewStats(employeeId);
        return Result.success(statsVO);
    }

    /**
     * 获取用户评价列表
     */
    @GetMapping("/user")
    @PreAuthorize("hasRole('USER')")
    public Result<Page<Review>> getUserReviews(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        // 获取当前用户ID
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        
        // 从用户名中提取用户ID（实际项目中应该从UserDetails中获取）
        Integer userId = getUserIdFromUsername(username);
        
        Page<Review> reviewPage = reviewService.getUserReviews(userId, page, size);
        return Result.success(reviewPage);
    }

    /**
     * 从用户名中提取用户ID
     * 实际项目中应该从UserDetails中获取
     */
    private Integer getUserIdFromUsername(String username) {
        // 这里简化处理，实际项目中应该从UserDetails中获取
        return 1;
    }
} 